package com.yohann.designPattern.command;

/**
 * <p>
 * 遥控器
 * </p>
 *
 * @author Yohann
 * @since 2021/2/11 16:32
 */
public class RemoteController {
    private Command[] onCommand;
    private Command[] offCommand;
    private Command undoCommand;

    public RemoteController() {
        onCommand = new Command[5];
        offCommand = new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommand[i] = new NoCommand();
            offCommand[i] = new NoCommand();
        }
    }

    public void setCommand(int no, Command onCommand, Command offCommand) {
        this.onCommand[no] = onCommand;
        this.offCommand[no] = offCommand;
    }

    public void onButtonPushed(int no) {
        onCommand[no].execute();
        undoCommand = onCommand[no];
    }

    public void offButtonPushed(int no) {
        offCommand[no].execute();
        undoCommand = offCommand[no];
    }

    public void undoButtonPushed(int no) {
        undoCommand.undo();
    }
}